﻿using System.Linq.Expressions;
using Basic.FormModel.DB;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.Control.Model;
using Basic.FormRemoteModel.Form.Model;
using Basic.FormRemoteModel.Page.Model;
using Basic.FormRemoteModel.PhysicalGroup.Model;
using LinqKit;
using WeDonekRpc.Helper;

namespace Basic.FormDAL.Repository
{
    internal static class WhereLinq
    {
        public static Expression<Func<DBCustomPage, bool>> ToWhere ( this PageQueryParam query )
        {
            ExpressionStarter<DBCustomPage> where = PredicateBuilder.New<DBCustomPage>(a => a.CompanyId == query.CompanyId);
            if ( query.QueryKey.IsNotNull() )
            {
                where = where.And(a => a.PageName.Contains(query.QueryKey));
            }
            if ( query.PageClassify.IsNotNull() )
            {
                where = where.And(a => a.PageClassify == query.PageClassify);
            }
            if ( !query.Status.IsNull() )
            {
                where = where.And(a => query.Status.Contains(a.Status));
            }
            if ( where.IsStarted )
            {
                return where;
            }
            return null;
        }
        public static Expression<Func<DBPhysicalFormGroup, bool>> ToWhere ( this PhysicalGroupQuery query )
        {
            ExpressionStarter<DBPhysicalFormGroup> where = PredicateBuilder.New<DBPhysicalFormGroup>();
            if ( query.QueryKey.IsNotNull() )
            {
                where = where.And(a => a.GroupName.Contains(query.QueryKey));
            }
            if ( query.IsEnable.HasValue )
            {
                where = where.And(a => a.IsEnable == query.IsEnable.Value);
            }
            if ( where.IsStarted )
            {
                return where;
            }
            return null;
        }
        public static Expression<Func<DBCustomControl, bool>> ToWhere ( this ControlQuery query )
        {
            ExpressionStarter<DBCustomControl> where = PredicateBuilder.New<DBCustomControl>();
            if ( query.QueryKey.IsNotNull() )
            {
                where = where.And(a => a.Name.Contains(query.QueryKey));
            }
            if ( query.ControlType.HasValue )
            {
                where = where.And(a => a.ControlType == query.ControlType.Value);
            }
            if ( query.FuncType.HasValue )
            {
                where = where.And(a => a.FuncType == query.FuncType.Value);
            }
            if ( !query.Status.IsNull() )
            {
                where = where.And(a => query.Status.Contains(a.Status));
            }
            if ( where.IsStarted )
            {
                return where;
            }
            return null;
        }
        public static Expression<Func<DBCustomForm, bool>> ToWhere ( this FormQuery query )
        {
            ExpressionStarter<DBCustomForm> where = PredicateBuilder.New<DBCustomForm>(a => a.CompanyId == query.CompanyId);
            if ( query.QueryKey.IsNotNull() )
            {
                where = where.And(a => a.FormName.Contains(query.QueryKey));
            }
            if ( query.FormClassify.IsNotNull() )
            {
                where = where.And(a => a.FormClassify.StartsWith(query.FormClassify));
            }
            if ( query.PhyGroupId.HasValue )
            {
                where = where.And(a => a.FormType == FormType.代理表单 && a.PhyGroupId == query.PhyGroupId);
            }
            else if ( !query.FormType.IsNull() )
            {
                where = where.And(a => query.FormType.Contains(a.FormType));
            }
            if ( !query.Status.IsNull() )
            {
                where = where.And(a => query.Status.Contains(a.FormStatus));
            }
            if ( query.IsLock.HasValue )
            {
                where = where.And(a => a.IsLock == query.IsLock.Value);
            }
            return where.And(a => a.IsDrop == false);
        }
    }
}
